package com.dhcc.core.framework.base.warpper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dhcc.core.framework.util.Bean2MapUtil;

/**
 * 控制器查询结果的包装类基类
 * 
 * @ClassName: BaseControllerWarpper
 * @Description: TODO
 * @author: cyf
 * @date: 2018年1月2日 上午9:45:12
 */
public abstract class BaseControllerWarpper {
    private Logger log = LoggerFactory.getLogger(this.getClass());

    public Object obj = null;

    public BaseControllerWarpper(Object obj) {
        this.obj = obj;
    }

    @SuppressWarnings("unchecked")
    public Object warp() {
        if (this.obj instanceof List) {
            List<Object> list = (List<Object>) this.obj;
            List<Object> rs = new ArrayList<Object>();
            for (Object map : list) {
                Map<String, Object> tmp = null;
                if (map instanceof Map) {
                    tmp = (Map<String, Object>) map;
                } else {
                    try {
                        tmp = (Map<String, Object>) Bean2MapUtil.toMap(map);
                    } catch (Exception e) {
                        log.error("Object转Map失败。。。");
                        continue;
                    }
                }
                warpTheMap(tmp);
                rs.add(tmp);
            }
            return rs;
        } else if (this.obj instanceof Map) {
            Map<String, Object> map = (Map<String, Object>) this.obj;
            warpTheMap(map);
            return map;
        } else {
            Map<String, Object> tmp = (Map<String, Object>) Bean2MapUtil.toMap(this.obj);
            warpTheMap(tmp);
            return tmp;
        }
    }

    protected abstract void warpTheMap(Map<String, Object> map);
}
